﻿#region License

//     This file is part of TCP Sender.
// 
//     TCP Sender is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     TCP Sender is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with TCP Sender.  If not, see <http://www.gnu.org/licenses/>

#endregion

/*
 * Created by Error218
 * 25/07/2012
 * 22:29
 */


using System;
using System.Net;
using System.Text;
using System.Windows.Forms;
using PacketDotNet;
using SharpPcap;
using SharpPcap.LibPcap;

namespace Packet_Sender
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();          // Drawing the form...
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            // Getting external IP /*
            try
            {
                WebClient webClient = new WebClient();
                Globals.externIP = webClient.DownloadString("http://automation.whatismyip.com/n09230945.asp");          // Setting a global variable who contains external IP...
                txtSourceIP.Text = Globals.externIP;        // Writing external IP to the textbox...
            }
            catch (Exception ex)
            {
                // Exception case /*
                MessageBox.Show("An error as occurred:\n\nError in " + ex.Source + "\n\n" + ex.Message + "\n\n" + ex.StackTrace);
                // Exception case */
            }
            // Getting external IP */
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            // Elaborate and send the packet:   
            try
            {
                // Elaborating packets /*
                TcpPacket tcpPacket = new TcpPacket(Convert.ToUInt16(txtSourcePort.Text), Convert.ToUInt16(txtDestinationPort.Text));       // Elaborating TCP Packet...
                IPv4Packet ipPacket = new IPv4Packet(System.Net.IPAddress.Parse(txtSourceIP.Text), System.Net.IPAddress.Parse(txtDestIP.Text));         // Elaborating IP Packet...
                EthernetPacket ethernetPacket = new EthernetPacket(System.Net.NetworkInformation.PhysicalAddress.Parse(txtSourceHW.Text), System.Net.NetworkInformation.PhysicalAddress.Parse(txtDestinationHW.Text), EthernetPacketType.None);         // Elaborating Ethernet Packet...

                tcpPacket.PayloadData = UTF8Encoding.UTF8.GetBytes(txtData.Text);       // Loading the data from the textbox...
                ipPacket.PayloadPacket = tcpPacket;             // Loading TCP packet in IP packet...
                ethernetPacket.PayloadPacket = ipPacket;        // Loading IP packet (who contain TCP packet) in Ethernet packet (last packet)...
                // Elaborating packets */

                // Sending the packet /*
                Globals.currentDevice.Open();       // Opening TCP connection...
                Globals.currentDevice.SendPacket(ethernetPacket);       // Sending the packet...
                Globals.currentDevice.Close();      // Ending TCP connection...
                // Sending the packet */
            }
            catch (Exception ex)
            {
                // Exception case /*
                MessageBox.Show("An error as occurred:\n\nError in " + ex.Source + "\n\n" + ex.Message + "\n\n" + ex.StackTrace);
                // Exception case */
            }
            
        }
    }
}
